package com.zytlifeservice.controller;

import com.zytlifeservice.domain.Result;
import com.zytlifeservice.service.FileUploadService;
import com.zytlifeservice.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * Created by MJS on 2017/4/24.
 */

@Controller
@RequestMapping(value = "/file")
public class FileUploadController {

    private static final Logger logger = LoggerFactory.getLogger(FileUploadController.class);

    @Autowired
    private FileUploadService fileUploadService;

    @Value("${web.upload-path}")
    private String uploadPath;

    @GetMapping(value = "/index")
    public String index(Model model) {
        //获取已存在的文件
        File[] files = new File(uploadPath).listFiles();
        model.addAttribute("files", files);
        return "web/index";
    }

    @PostMapping(value = "/upload")
    @ResponseBody
    public Result store(@RequestParam("userId") String userId, @RequestParam("headimg") MultipartFile[] files) {
        //这里，要上传userID的，头像统一管理，名字用userId+str判定，如果存在的话，则删除之前的头像。
        //头像仓库的处理。
        //userID的取值。
        if (files != null && files.length >= 1) {
            return ResultUtil.success(fileUploadService.store(userId, files[0]), "上传成功");
        } else {
            return ResultUtil.error(100, "上传失败");
        }
    }

    //区分银行，游戏的上传方式。
    @PostMapping(value = "/upload/gameOrBank")
    @ResponseBody
    public Result storeBankOrGame(@RequestParam("image") MultipartFile[] files) {
        if (files != null && files.length >= 1) {
            return ResultUtil.success(fileUploadService.storeBaseIcon(files[0]), "上传成功");
        } else {
            return ResultUtil.error(100, "上传失败");
        }
    }

    @GetMapping("/files/headIcon/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
        Resource file = fileUploadService.loadAsResource(filename);
        return ResponseEntity
                .ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
                .body(file);
    }

    @GetMapping("/files/baseIcon/{filename:.+}")
    public ResponseEntity<Resource> serveBaseIconFile(@PathVariable String filename) {
        Resource fileResource = fileUploadService.loadAsBaseIconResource(filename);
        return ResponseEntity
                .ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileResource.getFilename() + "\"")
                .body(fileResource);
    }

}
